package com.ordersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ordersystem.domain.Order;
import com.ordersystem.domain.OrderManageData;
import com.ordersystem.mappers.DishMapper;
import com.ordersystem.mappers.OrderMapper;
import com.ordersystem.mappers.ShoppingMapper;
import com.ordersystem.mappers.UserMapper;
import com.ordersystem.service.DishService;
import com.ordersystem.service.OrderService;
import com.ordersystem.utils.CommonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : OrderServiceImpl
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/26 17:39]
 * @updateUser : asus1020
 * @updateTime : [2022/3/26 17:39]
 * @updateRemark : [描述说明本次修改内容]
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    CommonUtils commonUtils;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private ShoppingMapper shoppingMapper;
    @Resource
    private DishService dishService;
    @Resource
    private UserMapper userMapper;
    @Override//添加订单
    @Transactional
    public int addOrder(Order order) {
        ArrayList<Integer> didsList = new ArrayList<>();
        String[] dids = order.getDids().split(",");
        for (String did : dids) {
            didsList.add(Integer.valueOf(did));
        }
        List<Integer> orderDidList = shoppingMapper.getShoppingGidByUidAndDids(order.getUid(), didsList);
        dishMapper.finishShoppingTrolley(order.getUid(),didsList);
        order.setGids(commonUtils.listToChangeString(orderDidList));
        return orderMapper.addOrder(order);
    }

    @Override//订单付款
    @Transactional
    public int parOrder(Integer oid,Integer state) {
        //准备支付
        if (state ==1) {
            Order orderByOid = orderMapper.findOrderByOid(oid);
            List<Integer> gidList = commonUtils.stringToChangeList(orderByOid.getGids(), ",");
            for (Integer gidItem : gidList) {
                dishMapper.updateDishSellVolume(gidItem);
            }
        }
        return orderMapper.payOrder(oid,state);
    }

    @Override//订单结束
    public int finishOrder(Integer oid,boolean state) {
        return orderMapper.finishOrder(oid,state);
    }

    @Override//根据条件查询订单
    public Map<String,Object> findOrders(String query, Integer type, Integer pagenum, Integer pagesize) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Object> pageInfo = PageHelper.startPage(pagenum, pagesize);
        List<Order> orders = orderMapper.findOrders(query, type);
        for (Order orderItem : orders) {
            orderItem.setDishList(dishService.findDishDetailByOid(orderItem.getOid()));
            orderItem.setUser(userMapper.findUserById(orderItem.getUid()));
        }
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        map.put("total",total);
        map.put("pagenum",pageNum);
        map.put("orders",orders);
        return map;
    }

    @Override
    public List<Order> findOrdersByUid(Integer uid) {
        return orderMapper.findOrdersByUid(uid);
    }

    @Override
    public List<OrderManageData> getOrderManageData(Integer dataNum) {
        return orderMapper.getOrderManageData(dataNum);
    }
}
